<?php
/**
 * Action object to be used with the nuc_Controller classes.
 * 
 * @package		Nuclide-Mvc
 * @version		$Id$
 */

Zend_Loader::loadClass('Zend_Controller_Action');
Zend_Loader::loadClass('Zend_Controller_Action_Exception');

/**
 * Action object to be used with the nuc_Controller classes.
 * 
 * @package		Nuclide-Mvc
 */
class nuc_Controller_Action extends Zend_Controller_Action {
	/**
	 * Basically the same as forward only that it exits dispatch loop even if you after the preDispatch stage.
	 * Should be followed by a return; to avoid unexpected behaviour.
	 *
	 * @param	string		Action name
	 * @param	string		Controller name
	 * @param	string		OPTIONAL Module name
	 * @param	array		OPTIONAL Parameters
	 */
	public function _dispatchForward( $action, $controller, $module = null, $params = array() ) {
		$this->getRequest()->setDispatched( false );
		$request = $this->getRequest();
		$request->setControllerName( $controller );
		$request->setActionName( $action );
		$request->setModuleName( $module );
		$request->setParams( $params );

		$this->setRequest( $request );
		$this->dispatch( $action );
	}
	
	/**
	 * Overloaded action object calls
	 *
	 * @param	string	Class Name
	 * @param 	array	Arguments
	 */
	public function __call( $className, $args ) {
		// Can we load desired action object class?
		if( !$this->getFrontController()->getDispatcher()->getClassLoader()->classLoadable( $className ) ) {
			throw new Zend_Controller_Action_Exception("Class '$className' cannot be loaded.");
		}
		
		// Load it with classLoader from Dispatcher. Same loader that loads controller code.
		$this->getFrontController()->getDispatcher()->getClassLoader()->loadClass( $className );
		
		// Execute action object.
		$obj = new $className( $args, $this );
		if( method_exists( $obj, 'isCancelled' ) == false || $obj->isCancelled() == false ) {
			$obj->execute();
		}
		
		if( method_exists( $obj, '__finish' ) ) {		
			$obj->__finish();
		}
	}
}
?>